package com.chuangjiangx.member.manager.client.web.stored.controller;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.request.Page;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlinePageResponse;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.basic.web.interceptor.Permissions;
import com.chuangjiangx.member.manager.client.web.stored.request.SearchStoredInfoRequest;
import com.chuangjiangx.member.manager.client.web.stored.request.StoredInfoListForMerchantRequest;
import com.chuangjiangx.member.manager.client.web.stored.request.StoredInfoListForStoreRequest;
import com.chuangjiangx.member.manager.client.web.stored.response.StoredDetailInfoForClientResponse;
import com.chuangjiangx.member.manager.client.web.stored.response.StoredDetailListForClientResponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.common.utils.StrUtils;
import com.chuangjiangx.member.stored.ddd.dal.condition.QueryStoredListForClientCondition;
import com.chuangjiangx.member.stored.ddd.query.MbrStoredStreamClientQuery;
import com.chuangjiangx.member.stored.ddd.query.dto.StoredStreamInfoForClientDTO;
import com.chuangjiangx.member.stored.ddd.query.dto.StoredStreamListForClientDTO;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 储值明细(此接口类客户端未发现有使用)
 */
@Api(value = "储值明细(此接口类客户端未发现有使用)",tags = "储值")
@RestController
@RequestMapping(value = "/app/member/stored", produces = "application/json")
public class MbrStoredInfoController extends BaseController {
    @Autowired
    private  MbrStoredStreamClientQuery mbrStoredStreamClientQuery;


    /**
     * 商户查询储值明细列表
     *
     * @param merchantRequest 查询参数
     * @return 明细列表
     * @map data
     * @see StoredDetailListForClientResponse
     */
    @ApiOperation(value = "查询商户的储值明细列表-商户权限")
    @RequestMapping(value = "/searchStoredDetailListForMerchant",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("55001")
    public UnderlinePageResponse searchStoredDetailListForMerchant(@Validated @RequestBody StoredInfoListForMerchantRequest merchantRequest,
                                                                   HttpServletRequest request) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        //参数转换
        QueryStoredListForClientCondition clientCondition = new QueryStoredListForClientCondition();
        BeanUtils.convertBean(merchantRequest, clientCondition);
        //获取分页信息
        Page page = new Page();
        page.setPageNO(merchantRequest.getPageNumber());
        page.setEveryPageCount(merchantRequest.getPageSize());

        clientCondition.setPageNumber(merchantRequest.getPageNumber());
        clientCondition.setPageSize(merchantRequest.getPageSize());
        clientCondition.setMerchantId(user.getMerchantId());

        PagingResult<StoredStreamListForClientDTO> pagingResult = mbrStoredStreamClientQuery.searchStoredStreamListForClient(clientCondition);
        List<StoredDetailListForClientResponse> respList = BeanUtils.convertCollection(pagingResult.getItems(),
                StoredDetailListForClientResponse.class, (source, target) -> {
            target.setMobile(StrUtils.encodeMobilePhone(source.getMobile()));
        });

        return (UnderlinePageResponse)ResponseUtils.successPage(page, pagingResult, "storedDetailList", respList);
    }

    /**
     * 门店查询储值明细列表
     *
     * @param storeRequest 查询参数
     * @return 明细列表
     * @map data
     * @see StoredDetailListForClientResponse
     */
    @ApiOperation(value = "查询门店的储值明细列表-门店权限")
    @RequestMapping(value = "/searchStoredDetailListForStore",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("56001")
    public UnderlinePageResponse searchStoredDetailListForStore(@Validated @RequestBody StoredInfoListForStoreRequest storeRequest,
                                                   HttpServletRequest request) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        //参数转换
        QueryStoredListForClientCondition clientCondition = new QueryStoredListForClientCondition();
        BeanUtils.convertBean(storeRequest, clientCondition);

        //获取分页信息
        Page page = new Page();
        page.setPageNO(storeRequest.getPageNumber());
        page.setEveryPageCount(storeRequest.getPageSize());

        clientCondition.setPageNumber(storeRequest.getPageNumber());
        clientCondition.setPageSize(storeRequest.getPageSize());
        //以下两项为必传参数，只能查询所属的相关信息
        clientCondition.setStoreId(user.getStoreId());
        clientCondition.setMerchantId(user.getMerchantId());

        PagingResult<StoredStreamListForClientDTO> pagingResult = mbrStoredStreamClientQuery.searchStoredStreamListForClient(clientCondition);
        List<StoredDetailListForClientResponse> respList = BeanUtils.convertCollection(pagingResult.getItems(),
                StoredDetailListForClientResponse.class,
                (source, target) -> {
            target.setMobile(StrUtils.encodeMobilePhone(source.getMobile()));
        });

        return (UnderlinePageResponse)ResponseUtils.successPage(page, pagingResult, "storedDetailList", respList);
    }

    /**
     * 根据id查询储值明细详情
     */
    @RequestMapping(value = "/searchStoredDetail",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("59012")
    @ApiOperation(value = "根据储值明细id查询储值明细详情" )
    public Response<StoredDetailInfoForClientResponse> searchStoredDetailInfo(@Validated @RequestBody SearchStoredInfoRequest request) {
        StoredDetailInfoForClientResponse response = new StoredDetailInfoForClientResponse();
        StoredStreamInfoForClientDTO clientDTO = mbrStoredStreamClientQuery.searchStoredStreamInfoForClient(request.getId());
        BeanUtils.convertBean(clientDTO, response);
        return ResponseUtils.success("clientStoredInfo", response);
    }

}
